import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_lib/utils/DpUtils.dart';
import 'package:flutter_lib/utils/LogUtils.dart';
import 'package:flutter_my_web/bean/response/GetMusicListByTagResponse.dart';
import 'package:flutter_my_web/bean/response/get_music_tag_response.dart';
import 'package:flutter_my_web/constants/constant_status.dart';
import 'package:flutter_my_web/widget/TextWidget.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'music_controller.dart';

class music_menu_tab_bar_view_widget extends StatefulWidget {
  GetMusicTagBean bean;

  music_menu_tab_bar_view_widget(this.bean);

  @override
  State<music_menu_tab_bar_view_widget> createState() =>
      _music_menu_tab_bar_view_widgetState();
}

class _music_menu_tab_bar_view_widgetState
    extends State<music_menu_tab_bar_view_widget>
    with AutomaticKeepAliveClientMixin {
  MusicController logic = Get.find();

  List<GetMusicListByTagResponseData> datas = [];

  Future<void> _getData() async {
    var musicListByTag = await logic.getMusicListByTag(widget.bean.name);
    datas = musicListByTag?.data ?? [];
    logic.refresh();
  }

  @override
  bool get wantKeepAlive => true;

  RefreshController _refreshController =
      RefreshController(initialRefresh: true);

  Future<void> _onRefresh() async {
    await _getData();
    _refreshController.refreshCompleted();
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MusicController>(
      initState: (s) {},
      builder: (logic) {
        return Scaffold(
            backgroundColor: Colors.black45,
            floatingActionButton: FloatingActionButton(
              child: Icon(Icons.refresh),
              onPressed: () {
                _getData();
              },
            ),
            // body:  ListView.separated(itemBuilder: (BuildContext context, int index){
            //   return music_menu_tab_bar_view_widget_item_widget(datas[index]);
            // }, separatorBuilder: (BuildContext context, int index) {
            //   return Divider(color: Colors.white,);
            // }, itemCount: datas.length),
            body: SmartRefresher(
              enablePullDown: true,
              enablePullUp: true,
              header: WaterDropHeader(),
              controller: _refreshController,
              onRefresh: _onRefresh,
              child: ListView.separated(
                  itemBuilder: (BuildContext context, int index) {
                    return music_menu_tab_bar_view_widget_item_widget(
                        datas[index]);
                  },
                  separatorBuilder: (BuildContext context, int index) {
                    return Divider(
                      color: Colors.white,
                    );
                  },
                  itemCount: datas.length),
            ));
      },
    );
  }
}

class music_menu_tab_bar_view_widget_item_widget extends StatefulWidget {
  GetMusicListByTagResponseData data;

  music_menu_tab_bar_view_widget_item_widget(this.data);

  @override
  State<music_menu_tab_bar_view_widget_item_widget> createState() =>
      _music_menu_tab_bar_view_widget_item_widgetState();
}

class _music_menu_tab_bar_view_widget_item_widgetState
    extends State<music_menu_tab_bar_view_widget_item_widget> {
  bool isHover = false;

  @override
  Widget build(BuildContext context) {
    var playStatusIcon = Icon(
      Icons.numbers,
      color: Colors.transparent,
    );
    if (widget.data.playStatusEnum == PlayStatusEnum.no_play) {
      playStatusIcon = Icon(Icons.numbers, color: Colors.transparent);
    } else if (widget.data.playStatusEnum == PlayStatusEnum.play_ing) {
      playStatusIcon =
          Icon(Icons.music_note, color: isHover ? Colors.red : Colors.white);
    } else if (widget.data.playStatusEnum == PlayStatusEnum.stop) {
      playStatusIcon =
          Icon(Icons.stop, color: isHover ? Colors.red : Colors.white);
    }
    return InkWell(
        onHover: (hover) {
          isHover = hover;
          setState(() {});
        },
        onTap: () async {
          MusicController logic = Get.find();
          if (widget.data.playStatusEnum == PlayStatusEnum.no_play) {
            widget.data.playStatusEnum = PlayStatusEnum.play_ing;
            logic.curPlayMusicBean?.playStatusEnum = PlayStatusEnum.no_play;
            logic.curPlayMusicBean = widget.data;
            await logic.audioPlugin.play(
                UrlSource(widget.data.netPath!.replaceAll("http:", "https:")));
            var duration = await logic.audioPlugin.getDuration();
            logic.curMusicAllDuration = duration!.inSeconds;
            LogUtils.e(
                "play duration:${duration.inSeconds}  url:${widget.data.netPath!}  ");
          } else if (widget.data.playStatusEnum == PlayStatusEnum.play_ing) {
            widget.data.playStatusEnum = PlayStatusEnum.stop;
            logic.audioPlugin.pause();
          } else if (widget.data.playStatusEnum == PlayStatusEnum.stop) {
            widget.data.playStatusEnum = PlayStatusEnum.play_ing;
            logic.audioPlugin.resume();
          }
          logic.refresh();
        },
        child: Container(
          padding: EdgeInsets.all(8.0.wDp),
          child: Row(
            children: [
              playStatusIcon,
              SimpleText(
                "${widget.data.name}",
                color: isHover ? Colors.red : Colors.white,
              ),
            ],
          ),
        ));
  }
}
